import 'package:flutter/material.dart';
import '../models/exhibition.dart';
import '../utils/theme_colors.dart';

class ExhibitionFilterSheet extends StatefulWidget {
  final ExhibitionFilter currentFilter;
  final Function(ExhibitionFilter) onFilterChanged;

  const ExhibitionFilterSheet({
    Key? key,
    required this.currentFilter,
    required this.onFilterChanged,
  }) : super(key: key);

  @override
  State<ExhibitionFilterSheet> createState() => _ExhibitionFilterSheetState();
}

class _ExhibitionFilterSheetState extends State<ExhibitionFilterSheet> {
  late ExhibitionFilter _selectedFilter;

  @override
  void initState() {
    super.initState();
    _selectedFilter = widget.currentFilter;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 顶部拖拽指示器
          Container(
            margin: const EdgeInsets.only(top: 12),
            width: 40,
            height: 4,
            decoration: BoxDecoration(
              color: Colors.grey[300],
              borderRadius: BorderRadius.circular(2),
            ),
          ),
          // 标题
          Padding(
            padding: const EdgeInsets.all(20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const Text(
                  '筛选展览',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                TextButton(
                  onPressed: () {
                    setState(() {
                      _selectedFilter = ExhibitionFilter.all;
                    });
                  },
                  child: const Text('重置'),
                ),
              ],
            ),
          ),
          // 筛选选项
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  '展览类型',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const SizedBox(height: 12),
                _buildFilterOptions(),
                const SizedBox(height: 20),
                const Text(
                  '展览状态',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const SizedBox(height: 12),
                _buildStatusOptions(),
                const SizedBox(height: 20),
                const Text(
                  '特殊筛选',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const SizedBox(height: 12),
                _buildSpecialOptions(),
              ],
            ),
          ),
          // 底部按钮
          Container(
            padding: const EdgeInsets.all(20),
            child: Row(
              children: [
                Expanded(
                  child: OutlinedButton(
                    onPressed: () => Navigator.pop(context),
                    style: OutlinedButton.styleFrom(
                      padding: const EdgeInsets.symmetric(vertical: 12),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: const Text('取消'),
                  ),
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: ElevatedButton(
                    onPressed: () {
                      widget.onFilterChanged(_selectedFilter);
                      Navigator.pop(context);
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: ThemeColors.primary,
                      padding: const EdgeInsets.symmetric(vertical: 12),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: const Text(
                      '确定',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ),
              ],
            ),
          ),
          // 底部安全区域
          SizedBox(height: MediaQuery.of(context).padding.bottom),
        ],
      ),
    );
  }

  Widget _buildFilterOptions() {
    final typeFilters = [
      ExhibitionFilter.all,
      ExhibitionFilter.traditional,
      ExhibitionFilter.modern,
      ExhibitionFilter.interactive,
      ExhibitionFilter.virtual,
    ];

    return Wrap(
      spacing: 8,
      runSpacing: 8,
      children: typeFilters.map((filter) {
        final isSelected = _selectedFilter == filter;
        return GestureDetector(
          onTap: () {
            setState(() {
              _selectedFilter = filter;
            });
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            decoration: BoxDecoration(
              color: isSelected ? ThemeColors.primary : Colors.grey[100],
              borderRadius: BorderRadius.circular(20),
              border: Border.all(
                color: isSelected ? ThemeColors.primary : Colors.grey[300]!,
              ),
            ),
            child: Text(
              filter.displayName,
              style: TextStyle(
                color: isSelected ? Colors.white : ThemeColors.textPrimary,
                fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
              ),
            ),
          ),
        );
      }).toList(),
    );
  }

  Widget _buildStatusOptions() {
    final statusFilters = [
      ExhibitionFilter.ongoing,
      ExhibitionFilter.upcoming,
      ExhibitionFilter.ended,
    ];

    return Wrap(
      spacing: 8,
      runSpacing: 8,
      children: statusFilters.map((filter) {
        final isSelected = _selectedFilter == filter;
        return GestureDetector(
          onTap: () {
            setState(() {
              _selectedFilter = filter;
            });
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            decoration: BoxDecoration(
              color: isSelected ? ThemeColors.primary : Colors.grey[100],
              borderRadius: BorderRadius.circular(20),
              border: Border.all(
                color: isSelected ? ThemeColors.primary : Colors.grey[300]!,
              ),
            ),
            child: Text(
              filter.displayName,
              style: TextStyle(
                color: isSelected ? Colors.white : ThemeColors.textPrimary,
                fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
              ),
            ),
          ),
        );
      }).toList(),
    );
  }

  Widget _buildSpecialOptions() {
    final specialFilters = [
      ExhibitionFilter.free,
      ExhibitionFilter.bookmarked,
      ExhibitionFilter.hot,
    ];

    return Wrap(
      spacing: 8,
      runSpacing: 8,
      children: specialFilters.map((filter) {
        final isSelected = _selectedFilter == filter;
        return GestureDetector(
          onTap: () {
            setState(() {
              _selectedFilter = filter;
            });
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
            decoration: BoxDecoration(
              color: isSelected ? ThemeColors.primary : Colors.grey[100],
              borderRadius: BorderRadius.circular(20),
              border: Border.all(
                color: isSelected ? ThemeColors.primary : Colors.grey[300]!,
              ),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Icon(
                  _getFilterIcon(filter),
                  size: 16,
                  color: isSelected ? Colors.white : ThemeColors.textPrimary,
                ),
                const SizedBox(width: 4),
                Text(
                  filter.displayName,
                  style: TextStyle(
                    color: isSelected ? Colors.white : ThemeColors.textPrimary,
                    fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
                  ),
                ),
              ],
            ),
          ),
        );
      }).toList(),
    );
  }

  IconData _getFilterIcon(ExhibitionFilter filter) {
    switch (filter) {
      case ExhibitionFilter.free:
        return Icons.money_off;
      case ExhibitionFilter.bookmarked:
        return Icons.bookmark;
      case ExhibitionFilter.hot:
        return Icons.local_fire_department;
      default:
        return Icons.filter_list;
    }
  }
}